package com.icss.ah.shop.service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShopDateSellService {

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	DecimalFormat format = new DecimalFormat("0.00");
	/**
	 * 获取昨日汇总销售数据
	 * @param query
	 * @return
	 */
	public HashMap queryShopSellData(HashMap query){
		HashMap map  = new HashMap();
		try {
			map = sqlSessionTemplate.selectOne("ShopDateSellBean.queryShopSellData",query);
			//记录日志
			infolog.info("ShopDateSellService|queryShopSellData|list:"+map.size());
		} catch (Exception e) {
			errlog.error("ShopDateSellService|queryShopSellData|异常：",e);
		}
		return map;
	}
	/**
	 * 获取图表数据
	 * @param query
	 * @return
	 */
	public List queryChartData(HashMap query){
		List list  = new ArrayList();
		try {
			list = sqlSessionTemplate.selectList("ShopDateSellBean.queryChartData",query);
			//记录日志
			infolog.info("ShopDateSellService|queryChartData|list:"+list.size());
		} catch (Exception e) {
			errlog.error("ShopDateSellService|queryChartData|异常：",e);
		}
		return list;
	}
	/**
	 * 格式化数据
	 * @param request
	 * @param name
	 * @param buffer
	 * @param list
	 */
	public void setNum(String analysisType, StringBuffer buffer, List list) {
		String title = "";
		String dataColumn = "";
		if("0".equals(analysisType)){
			title = "入账金额（￥）";
			dataColumn = "IN_MONEY";
		} else if("1".equals(analysisType)){
			title = "出账金额（￥）";
			dataColumn = "OUT_MONEY";
		} else if("2".equals(analysisType)){
			title = "净入账金额（￥）";
			dataColumn = "NET_MONEY";
		} else if("3".equals(analysisType)){
			title = "账户余额（￥）";
			dataColumn = "OVER_MONEY";
		}
		//设置图表标题
		buffer.append(title);
		//设置图表x轴坐标
		String xAxisStr = "[";
		//设置图表y轴数据
		String seriasStr = "[{name:'" + title + "',data:[";
		for (int i = 0; i < list.size(); i++) {
			Map map = (Map)list.get(i);
			//x轴坐标
			xAxisStr += "'" + map.get("CUR_DATE").toString() + "'";
			String money = map.get(dataColumn).toString();
			//y轴数据
			seriasStr += format.format(Double.valueOf(money));
			if(i != list.size()-1){
				xAxisStr += ",";
				seriasStr += ",";
			}
		}
		xAxisStr +="]";
		seriasStr += "]}]";
		buffer.append("#@" + xAxisStr + "#@" + seriasStr);
	}
}
